<template>
  <div class="app-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>WebSocket连接测试</span>
      </div>

      <!-- 连接状态 -->
      <el-row :gutter="20" class="mb20">
        <el-col :span="12">
          <websocket-status />
        </el-col>
        <el-col :span="12">
          <div class="server-info">
            <h4>服务器状态</h4>
            <p>在线连接数: {{ serverStatus.onlineCount }}</p>
            <p>服务状态: {{ serverStatus.isEnabled ? '启用' : '禁用' }}</p>
          </div>
        </el-col>
      </el-row>

      <!-- 控制按钮 -->
      <el-row :gutter="10" class="mb20">
        <el-col :span="24">
          <el-button type="primary" @click="connectWebSocket" :disabled="isConnected">
            连接WebSocket
          </el-button>
          <el-button type="danger" @click="disconnectWebSocket" :disabled="!isConnected">
            断开连接
          </el-button>
          <el-button type="info" @click="fetchServerStatus">
            刷新服务器状态
          </el-button>
          <el-button type="warning" @click="clearMessages">
            清空消息
          </el-button>
        </el-col>
      </el-row>

      <!-- 实时数据显示 -->
      <el-row :gutter="20">
        <el-col :span="12">
          <el-card>
            <div slot="header">设备状态数据</div>
            <el-table :data="deviceStatus" size="small" max-height="300">
              <el-table-column prop="deviceId" label="设备ID" width="120" />
              <el-table-column prop="status" label="状态" width="80">
                <template slot-scope="scope">
                  <el-tag :type="getStatusType(scope.row.status)" size="mini">
                    {{ scope.row.status }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="timestamp" label="时间" width="150">
                <template slot-scope="scope">
                  {{ formatTime(scope.row.timestamp) }}
                </template>
              </el-table-column>
            </el-table>
          </el-card>
        </el-col>

        <el-col :span="12">
          <el-card>
            <div slot="header">设备信息数据</div>
            <el-table :data="deviceInfo" size="small" max-height="300">
              <el-table-column prop="deviceId" label="设备ID" width="120" />
              <el-table-column prop="xRms" label="X轴RMS" width="80" />
              <el-table-column prop="yRms" label="Y轴RMS" width="80" />
              <el-table-column prop="zRms" label="Z轴RMS" width="80" />
              <el-table-column prop="timestamp" label="时间" width="150">
                <template slot-scope="scope">
                  {{ formatTime(scope.row.timestamp) }}
                </template>
              </el-table-column>
            </el-table>
          </el-card>
        </el-col>
      </el-row>

      <!-- 消息历史 -->
      <el-card class="mt20">
        <div slot="header">消息历史</div>
        <div class="message-history">
          <div
            v-for="(message, index) in messageHistory"
            :key="index"
            class="message-item"
            :class="getMessageClass(message.type)"
          >
            <div class="message-header">
              <span class="message-type">{{ message.type }}</span>
              <span class="message-time">{{ formatTime(message.timestamp) }}</span>
            </div>
            <div class="message-content">
              <pre>{{ JSON.stringify(message.data, null, 2) }}</pre>
            </div>
          </div>
        </div>
      </el-card>
    </el-card>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'
import WebSocketStatus from '@/components/WebSocketStatus'

export default {
  name: 'WebSocketTest',
  components: {
    WebSocketStatus
  },
  computed: {
    ...mapGetters('websocket', ['connectionInfo']),

    isConnected() {
      return this.connectionInfo.isConnected
    },

    serverStatus() {
      return this.$store.state.websocket.serverStatus
    },

    deviceStatus() {
      return this.$store.state.websocket.deviceStatus
    },

    deviceInfo() {
      return this.$store.state.websocket.deviceInfo
    },

    messageHistory() {
      return this.$store.state.websocket.messageHistory
    }
  },
  created() {
    this.fetchServerStatus()
  },
  methods: {
    ...mapActions('websocket', [
      'initWebSocket',
      'disconnectWebSocket',
      'fetchServerStatus'
    ]),

    async connectWebSocket() {
      try {
        await this.initWebSocket()
        this.$message.success('WebSocket连接成功')
      } catch (error) {
        this.$message.error('WebSocket连接失败: ' + error.message)
      }
    },

    clearMessages() {
      this.$store.commit('websocket/CLEAR_DATA')
      this.$message.success('消息已清空')
    },

    formatTime(timestamp) {
      if (!timestamp) return ''
      return new Date(timestamp).toLocaleString()
    },

    getStatusType(status) {
      switch (status) {
        case '正常':
        case 'online':
          return 'success'
        case '异常':
        case 'error':
          return 'danger'
        case '警告':
        case 'warning':
          return 'warning'
        default:
          return 'info'
      }
    },

    getMessageClass(type) {
      switch (type) {
        case 'DEVICE_STATUS_UPDATE':
          return 'status-message'
        case 'DEVICE_INFO_UPDATE':
          return 'info-message'
        default:
          return 'default-message'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.mb20 {
  margin-bottom: 20px;
}

.mt20 {
  margin-top: 20px;
}

.server-info {
  h4 {
    margin: 0 0 10px 0;
    color: #303133;
  }

  p {
    margin: 5px 0;
    color: #606266;
  }
}

.message-history {
  max-height: 400px;
  overflow-y: auto;

  .message-item {
    border: 1px solid #ebeef5;
    border-radius: 4px;
    margin-bottom: 10px;
    padding: 10px;

    &.status-message {
      border-left: 4px solid #67c23a;
    }

    &.info-message {
      border-left: 4px solid #409eff;
    }

    &.default-message {
      border-left: 4px solid #909399;
    }

    .message-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 8px;

      .message-type {
        font-weight: 500;
        color: #303133;
      }

      .message-time {
        font-size: 12px;
        color: #909399;
      }
    }

    .message-content {
      pre {
        background-color: #f5f7fa;
        padding: 8px;
        border-radius: 4px;
        font-size: 12px;
        margin: 0;
        white-space: pre-wrap;
        word-break: break-all;
      }
    }
  }
}
</style>
